package coolq.yaozhenyong.controller;

import java.util.List;

import com.forte.qqrobot.anno.Filter;
import com.forte.qqrobot.anno.Listen;
import com.forte.qqrobot.anno.depend.Beans;
import com.forte.qqrobot.beans.messages.types.MsgGetTypes;
import com.forte.qqrobot.component.forlemoc.beans.msgget.MsgGroup;
import com.forte.qqrobot.sender.MsgSender;
import com.forte.qqrobot.utils.CQCodeUtil;

import coolq.yaozhenyong.bean.GradeAdmin;
import coolq.yaozhenyong.bean.GradeAdministrator;
import coolq.yaozhenyong.bean.Lexicon;
import coolq.yaozhenyong.custom.HtmlAndQq;
import coolq.yaozhenyong.custom.SpringContextUtil;
import coolq.yaozhenyong.custom.TimeUtils;
import coolq.yaozhenyong.service.GradeAdminService;
import coolq.yaozhenyong.service.GradeAdministratorService;
import coolq.yaozhenyong.service.LexiconIllegalService;
import coolq.yaozhenyong.service.LexiconService;
import coolq.yaozhenyong.bean.LexiconIllegal;
@Beans
public class QQGroupRecordController {
	
	public GradeAdminService gService = SpringContextUtil.getBean(GradeAdminService.class);
	public LexiconService lservice = SpringContextUtil.getBean(LexiconService.class);
	public LexiconIllegalService iservice = SpringContextUtil.getBean(LexiconIllegalService.class);
	public GradeAdministratorService gradeservice = SpringContextUtil.getBean(GradeAdministratorService.class);
	/***
	 * 一.实现QQ语言库回复功能
	 * 
	 * 二.实现违禁词库的撤回功能
	 * @param msg
	 * @param msgsender
	 */
	@Listen(MsgGetTypes.groupMsg)
	@Filter	
	public void grouprecords(MsgGroup msg,CQCodeUtil cqCodeUtil,MsgSender msgsender) {
			/** 判断这个群机器人是否为开启状态 **/
			GradeAdmin gradeadmin = gService.query().eq("gradenumber", msg.getGroup()).eq("codes", "robot").eq("isgoto", "1").one();
			if(gradeadmin!=null) {
				//1.机器人应答功能
				lexicon(msg,cqCodeUtil,msgsender);
				
				LexiconIllegal(msg,msgsender);
			}
		
	}
	/*****
	 * 违禁词处理
	 * @param msg
	 * @param msgsender
	 */
	private void LexiconIllegal(MsgGroup msg, MsgSender msgsender) {
		//一.首先做模糊匹配
		List<LexiconIllegal> list = iservice.query().eq("qunnumber", msg.getGroup()).eq("type", "包含").eq("isuser", "1").list();
		for (LexiconIllegal lexiconIllegal : list) {
			if(msg.getMsg().indexOf(lexiconIllegal.getTextillegal())!=-1) {
				dispose(msg,lexiconIllegal,msgsender);
			}
		}
		//二.做完全匹配模式
		List<LexiconIllegal> list2 = iservice.query().eq("qunnumber", msg.getGroup()).eq("type", "完全").eq("isuser", "1").eq("textillegal", msg.getMsg()).list();
		for (LexiconIllegal lexiconIllegal : list2) {
			dispose(msg,lexiconIllegal,msgsender);
		}
	}
	/****
	 * 发违禁词进行处理！惩罚！
	 * @param msg 
	 * @param lexiconIllegal
	 * @param msgsender
	 */
	private void dispose(MsgGroup msg, LexiconIllegal lexiconIllegal, MsgSender msgsender) {
		//1.首先处理违规人员
		switch (lexiconIllegal.getCode()) {
		case "禁言":
			Long time = (long)lexiconIllegal.getTime();
			msgsender.SETTER.setGroupBan(msg.getGroup(), msg.getQQ(), time * 60);
			break;
		case "踢出":
			msgsender.SETTER.setGroupMemberKick(msg.getGroup(), "2058308326", false);
			break;
		case "撤回":
			msgsender.SETTER.setMsgRecall(msg.getId());
			break;
		default:
			break;
		}
		//2.私聊：通知群内代管其发生的情况
		if(lexiconIllegal.getIsgoto()) {
			List<GradeAdministrator> list = gradeservice.query().eq("qunnumber", msg.getGroup()).eq("lexiconillegal", "1").list();
			for (GradeAdministrator g : list) {
				String tomsgisgoto ="时间："+TimeUtils.getTime() + "\n在群["+msg.getGroup()+"]中；\n用户["+msg.getQQ()+"]\n出现违规行为；\n原话：["+msg.getMsg()+"]\n处理方式:"+lexiconIllegal.getCode();
				msgsender.SENDER.sendPrivateMsg(g.getQqnumber(),tomsgisgoto);
			}
		}
	}
	
	
	/************      机器人应答         ***************/
	
	
	/****
	 * 机器人应答
	 * @param msg
	 * @param msgsender
	 */
	private void lexicon(MsgGroup msg,CQCodeUtil cqCodeUtil, MsgSender msgsender) {
		//一.首先做模糊匹配
		//后期优化，因为数据库做不到这个效果
		List<Lexicon> list = lservice.query().eq("qunnumber", msg.getGroup()).eq("type", "模糊").eq("isuser", "1").list();
		for (Lexicon lexicon : list) {
			if(msg.getMsg().indexOf(lexicon.getAntistop())!=-1) {
				if(lexicon.getCode().equals("public")) {
					msgsender.SENDER.sendGroupMsg(msg.getGroup(), HtmlAndQq.getMsghtml(lexicon.getResponse(),cqCodeUtil,msg));
				}else if(lexicon.getCode().equals("private")) {
					msgsender.SENDER.sendPrivateMsg(msg.getQQ(), HtmlAndQq.getMsghtml(lexicon.getResponse(),cqCodeUtil,msg));
				}
				lexicon.setSum(lexicon.getSum()+1);
				lservice.updateById(lexicon);
			}
		}
		//二.做完全匹配模式
		List<Lexicon> list2 = lservice.query().eq("qunnumber", msg.getGroup()).eq("type", "完全").eq("isuser", "1").eq("antistop", msg.getMsg()).list();
		for (Lexicon lexicon : list2) {
			if(lexicon.getCode().equals("public")) {
				msgsender.SENDER.sendGroupMsg(msg.getGroup(), HtmlAndQq.getMsghtml(lexicon.getResponse(),cqCodeUtil,msg));
			}else if(lexicon.getCode().equals("private")) {
				msgsender.SENDER.sendPrivateMsg(msg.getQQ(), HtmlAndQq.getMsghtml(lexicon.getResponse(),cqCodeUtil,msg));
			}
			lexicon.setSum(lexicon.getSum()+1);
			lservice.updateById(lexicon);
		}
	}
	
	
	
}
